/*
 *  UI_MixPanel.cpp
 *
 *	Date: 2011-8-4
 *  Author: linshenqi
 */

#include "stdafx.h"
#include "./UI_Frame.h"

UI_MixPanel::UI_MixPanel(const CRect *pRect, const GRAPHICPROP *pProp)
	: GraphicBase(pRect, pProp)
	, m_ft(RGB(255, 255, 255))
{
	for(int i = 0; i < FMB_NUM; ++i)
	{
		m_pFMP[i] = new UI_FaderMatrixPanel;
		m_Str[i].Format("Out%d", i + 1);
	}

	m_lf.lfCharSet = DEFAULT_CHARSET;
	m_lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	m_lf.lfEscapement = 0;
	strcpy(m_lf.lfFaceName, FONT_CAPTION);
	m_lf.lfHeight = 20;
	m_lf.lfItalic = true;
	m_lf.lfOrientation = 0;
	m_lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
	m_lf.lfPitchAndFamily = DEFAULT_PITCH;
	m_lf.lfQuality = DEFAULT_QUALITY;
	m_lf.lfStrikeOut = false;
	m_lf.lfUnderline = false;
	m_lf.lfWidth = 0;
	m_lf.lfWeight = 0;
}

UI_MixPanel::~UI_MixPanel()
{
	for(int i = 0; i < FMB_NUM; ++i)
	{
		delete m_pFMP[i];
	}
}

bool UI_MixPanel::OnPaint(CPaintDC *pDC, CDC *pMemDC)
{
	GRAPHICPROP prop;
	prop.clrFill = COLOR_LINE;
	prop.clrFrame = COLOR_LINE;
	CRect rc;
	for(int i = 0; i < FMB_NUM; ++i)
	{
		this->DrawLine(&this->m_Line[i].Start, &this->m_Line[i].End, &prop, pDC, pDC);
		rc.SetRect(this->m_Line[0].Start.x - 24, 
				   this->m_Line[i].Start.y - 12, 
				   this->m_Line[0].Start.x - 16,
				   this->m_Line[i].Start.y - 4);
		this->DrawText(&m_lf, &rc, this->m_Str[i], COLOR_FONT, pDC, pDC);
	}
	for(int i = 0; i < FMB_NUM; ++i)
	{
		m_pFMP[i]->OnPaint(pDC, pMemDC);
	}
	return true;
}

bool UI_MixPanel::OnMouseMove(UINT nFlags, CPoint point)
{
	for(int i = 0; i < FMB_NUM; ++i)
	{
		m_pFMP[i]->OnMouseMove(nFlags, point);
	}
	return true;
}

bool UI_MixPanel::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	CRect rc;
	rc.SetRect(this->m_pRect->left, this->m_pRect->top, this->m_pRect->right + LEN_PANELWIDTH + 16, this->m_pRect->bottom);
	if(!rc.PtInRect(point))
		return false;

	for(int i = 0; i < FMB_NUM; ++i)
	{
		if(m_pFMP[i]->OnLButtonDblClk(nFlags, point))
			break;
	}
	return true;
}

void UI_MixPanel::SetBaseWindow(CWnd *pWnd)
{
	GraphicBase::SetBaseWindow(pWnd);

	for(int i = 0; i < FMB_NUM; ++i)
	{
		m_pFMP[i]->SetBaseWindow(pWnd);
		m_pFMP[i]->SetID(i);
		m_pFMP[i]->SetParentUI(this);
	}
}

void UI_MixPanel::SetRect(const CRect *pRect)
{
	GraphicBase::SetRect(pRect);

	int nWidth = (this->m_pRect->Width() - IP_PADDING * (FMB_NUM - 1)) / 8;
	CRect rc;

	for(int i = 0; i < FMB_NUM; ++i)
	{
		rc.SetRect(pRect->left + i * (IP_PADDING + nWidth), pRect->top, pRect->left + i * (IP_PADDING + nWidth) + nWidth, pRect->bottom);
		m_pFMP[i]->SetRect(&rc);
	}

	for(int i = 0; i < FMB_NUM; ++i)
	{
		this->m_Line[i].Start.x = m_pFMP[0]->m_pRect->left - IP_PADDING;
		this->m_Line[i].Start.y = m_pFMP[0]->m_pTB[i]->m_pRect->CenterPoint().y;
		this->m_Line[i].End.x = m_pFMP[7]->m_pRect->left;
		this->m_Line[i].End.y = m_pFMP[0]->m_pTB[i]->m_pRect->CenterPoint().y;
	}
}

void UI_MixPanel::ToggleMute(int nPID, int nID, int nStmt)
{
	if(nPID == 0)
	{
		for(int i = 0; i < 8; ++i)
		{
			m_pFMP[nID]->m_pTB[i]->m_bMute = !m_pFMP[nID]->m_pTB[i]->m_bMute;
			if(m_pFMP[nID]->m_pTB[i]->m_bMute)
			{
				m_pFMP[nID]->m_pTB[i]->SetBmp(BMP_MUTE_EN, m_pFMP[nID]->m_pTB[i]->m_pRect);
			}
			else
			{
				m_pFMP[nID]->m_pTB[i]->SetBmp(BMP_MIX_EN, m_pFMP[nID]->m_pTB[i]->m_pRect);
			}
			m_pFMP[nID]->m_pTB[i]->Redraw();
		}
	}
	else
	{}
}

bool UI_MixPanel::OnRButtonDown(UINT nFlags, CPoint point)
{
	CRect rc;
	rc.SetRect(this->m_pRect->left, this->m_pRect->top, this->m_pRect->right + LEN_PANELWIDTH + 16, this->m_pRect->bottom);
	if(!rc.PtInRect(point))
		return false;

	for(int i = 0; i < FMB_NUM; ++i)
	{
		if(m_pFMP[i]->OnRButtonDown(nFlags, point))
			break;
	}

	return true;
}

bool UI_MixPanel::OnRButtonUp(UINT nFlags, CPoint point)
{
	CRect rc;
	rc.SetRect(this->m_pRect->left, this->m_pRect->top, this->m_pRect->right + LEN_PANELWIDTH + 16, this->m_pRect->bottom);
	if(!rc.PtInRect(point))
		return false;

	for(int i = 0; i < FMB_NUM; ++i)
	{
		if(m_pFMP[i]->OnRButtonUp(nFlags, point))
			break;
	}

	return true;
}